<?php
	require_once dirname(BASEPATH) . '/application/controllers/system/_init.php';
	
	abstract class abs_register extends _init {
		
		function index()
		{			
			$this->load->model('local/data/area/mod_province');
			$data['province_list'] = $this->mod_province->get_province_for_dropdownlist(false);
			
			$this->load->library('define/general_define');
			$data['facebook_api_id'] = $this->general_define->facebook_app_id;
								
			$this->template->write('content_1', $this->load->view('home/frm_register', $data, true));			
			$this->template->render();
		}
		function fb_submit(){
			$this->load->library('define/general_define');
			$this->load->library('facebook_api');
			$this->load->library('custom_datetime');
			$this->load->library('string_function');
			
			$this->facebook_api->facebook_app_id = $this->general_define->facebook_app_id;
			$this->facebook_api->facebook_secret = $this->general_define->facebook_secret;
			$response = $this->facebook_api->parse_signed_request($_REQUEST['signed_request']);
						
			$o_name = $response['registration']['name'];
			$name_arr = explode(' ', $o_name);
			
			$mem_fname = '';
			$mem_sname = '';
			if (count($name_arr) > 0) {
				$mem_fname = $name_arr[0];
				$mem_sname = $name_arr[1];
			}
			else
				$mem_fname = $o_name;
				
			$mem_username = $response['registration']['email'];
			
			$gen_pass = $this->string_function->random_password();
			$mem_pass = $this->string_function->provide_password($gen_pass);
			$mem_regis_date = $this->custom_datetime->current_date();
			$mem_address = $response['registration']['location']['name'];
			$mem_birthdate = $response['registration']['birthday'];
			$mem_email = $response['registration']['email'];
			$mem_fb_uid = $response['user_id'];	
								
			$this->load->model('local/data/mod_member');			
			if (!$this->mod_member->check_if_member_registered_on_facebook($mem_fb_uid)) {
				$this->mod_member->MEM_FNAME = $mem_fname;
				$this->mod_member->MEM_SNAME = $mem_sname;
				$this->mod_member->MEM_USERNAME = $mem_username;
				$this->mod_member->MEM_PASS = $mem_pass;
				$this->mod_member->MEM_REGIS_DATE = $mem_regis_date;
				$this->mod_member->MEM_ADDRESS = $mem_address;
				$this->mod_member->MEM_EMAIL = $mem_email;				
				$this->mod_member->MEM_BIRTH_DATE = $mem_birthdate;
				$this->mod_member->MEM_FB_UID = $mem_fb_uid;
				
				if ($this->mod_member->add())
					$this->submit_fb_redirect($mem_username, $gen_pass);			
			}
			else
				$this->auto_signin($mem_fb_uid); // auto sign in if user has registered
		}
		private function auto_signin($facebook_uid){
			$this->load->model('local/data/mod_member');
			$result = $this->mod_member->get(array('MEM_FB_UID' => $facebook_uid),
														array('MEM_ID', 'MEM_USERNAME'));			
								
			$this->session->set_userdata(array('IS_LOGGED_IN'	=> true,
												'MEM_ID' 		=> $result[0]->MEM_ID,
												'USERNAME' 		=> $result[0]->MEM_USERNAME,
												'USTATE'		=> 'm'));
			header('Location:' . base_url());	
		}
		private function submit_fb_redirect($username, $password){
			$this->load->library('string_function');
			
			$html = '<form name="frm-fb-redirect" action="' . base_url() .'member/login/authen" method="post">';
			$html .= '<input type="hidden" name="username" value="' . $username . '" />';
			$html .= '<input type="hidden" name="password" value="' . $password . '" />';
			$html .= '</form>';
			$html .= '<script type="text/javascript">';
			$html .= 'document.forms["frm-fb-redirect"].submit();';
			$html .= '</script>';
			print $html;
		}
		
		function submit() {
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$email = $this->input->post('email');
			$fname = $this->input->post('fname');
			$sname = $this->input->post('sname');
			$b_day = $this->input->post('b-day');
			$b_month = $this->input->post('b-month');
			$b_year = $this->input->post('b-year');
			$person_id = $this->input->post('personid');
			$address = $this->input->post('address');
			$province_id = $this->input->post('province');
			$postcode = $this->input->post('postcode');
			$phone = $this->input->post('phone');
			
			$this->load->model('local/data/mod_member');
			$this->mod_member->MEM_USERNAME = $username;
			$this->mod_member->MEM_PASSWORD = $password;
			$this->mod_member->MEM_EMAIL = $email;
			$this->mod_member->MEM_FNAME = $fname;
			$this->mod_member->MEM_SNAME = $sname;
			
		}
	}

?>